<template>
  <div>
    <van-nav-bar
        :fixed="true"
        @click-left="onClickLeft"
    >
      <div class="van-nav-bar_title" slot="title">{{ $t('feedback.title') }}</div>
      <van-icon name="arrow-left" slot="left" class="nav_bar_icon"></van-icon>
    </van-nav-bar>
    <van-row class="main main-feed-back">
      <van-row>
        <div class="tip">{{ $t('feedback.tip') }}</div>
      </van-row>

      <van-row class="gap"></van-row>
      <van-row class="text">
        {{ $t('feedback.theme') }}
      </van-row>
      <van-divider class="divider"/>
      <van-row>
        <van-field
            v-model="inputTitle"
            type="textarea"
            :placeholder="$t('feedback.enterTheme')"
            maxlength="100"
            autosize
            show-word-limit
        ></van-field>
      </van-row>
      <van-row class="gap"></van-row>
      <van-row class="text">
        {{ $t('feedback.content') }}
      </van-row>
      <van-divider class="divider"/>
      <van-row>
        <van-field
            v-model="inputValue"
            type="textarea"
            :placeholder="$t('feedback.enterContent')"
            rows="4"
            maxlength="500"
            show-word-limit
        ></van-field>
      </van-row>
      <van-row class="gap"></van-row>
      <van-row class="text">
        {{ $t('feedback.image') }}
      </van-row>
      <van-divider class="divider"/>
      <van-row class="feedback_img">
        <div class="feedback_item" v-for="(img,index) of imgData" :key="index">
          <div class="img">
            <van-icon name="close" class="feedback_item_close" @click="imgRemove(img)"></van-icon>
            <van-image class="feedback_item_img" :src="img ? img : ''" @click="imgClick(index)"></van-image>
          </div>
        </div>
        <div class="feedback_item" v-if="imgData.length < 6" @click="add()">
          <div class="add">
            <van-icon name="plus" class="feedback_icon"></van-icon>
            <br/>
            {{ $t('feedback.add') }}
          </div>
        </div>
      </van-row>
      <van-row class="img-length">
        {{ imgData.length ? imgData.length + "/6" : "0/6" }}
      </van-row>
      <van-row class="btn">
        <van-button type="primary" size="large" color="#39C581" @click="sub" :loading="loading">
          {{ $t('feedback.submit') }}
        </van-button>
      </van-row>
      <van-image-preview
          v-model="show"
          :images="imgData ? imgData : ''"
          :showIndex="false"
          :startPosition="imgIndex"
      ></van-image-preview>
    </van-row>
  </div>

</template>

<script>
import { feedBack } from '@/api'

export default {
  name: "feed-back",
  data() {
    return {
      iconName: null,
      show: false,
      popText: null,
      loading: false,
      inputTitle: null,
      inputValue: null,
      stateBtn: false,
      imgData: [],
      imgIndex: null
    }
  },
  mounted() {
    this.loading = false;
  },
  methods: {
    onClickLeft() {
      // this.$bridge.goBack();
      this.$router.go(-1);
    },
    sub() {
      let _this = this;
      this.loading = true;
      if (!this.inputTitle) {
        this.$notify({
          type: 'warning',
          message: _this.$t('feedback.themeTip')
        });
        this.loading = false;
        return;
      }
      if (!this.inputValue) {
        this.$notify({
          type: 'warning',
          message: _this.$t('feedback.contentTip')
        });
        this.loading = false;
        return;
      }
      let fromData = new FormData();
      if (this.imgData.length > 0) {
        this.imgData.forEach((item) => {
          fromData.append('images', this.convertBase64UrlToBlob(item));
        });
      }
      let usrInfo = JSON.parse(window.sessionStorage["loginInfo"]);
      // let id=usrInfo['data']["id"]+"";
      fromData.append("title", this.inputTitle);
      // fromData.append("userId",id);
      fromData.append("content", this.inputValue);
      fromData.append("phoneNumber", usrInfo['phoneNumber']);
      feedBack(fromData).then(res => {
        this.loading = false;
        if (res.data.success) {
          // this.iconName='checked';
          // this.show=true;
          // this.popText=this.$t('feedback.success');
          this.$toast.success({
            message: this.$t('feedback.success'),
            icon: 'checked',
            mask: 'true',
            duration: '2000'
          });
          let time = setTimeout(() => {
            this.$bridge.goBack();
          }, 1000)
        } else {
          // this.iconName='clear';
          // this.show=true;
          // this.popText=this.$t('feedback.failure');
          this.$toast.success({
            message: this.$t('feedback.failure'),
            icon: 'clear',
            mask: 'true',
            duration: '2000'
          });
          let time = setTimeout(() => {
            this.$bridge.goBack();
          }, 1000)
        }
      }).catch(err => {
        this.loading = false;
      });
    },
    imgRemove(data) {
      this.imgData = this.imgData.filter(function (val) {
        return val !== data;
      });
    },
    imgClick(data) {
      this.show = true;
      this.imgIndex = data;
    },
    add() {
      this.take().then(data => {
        console.log("照片回调----imgchange---", data);
        if (data && data['img']) {
          const base64 = "data:image/jpeg;base64," + data['img'];
          if (this.imgData.indexOf(base64) === -1) {
            this['imgData'].push(base64);
          }
        }
      });
    },
    take() {
      return new Promise((resolve, reject) => {
        this.$bridge["openCamera"](null, res => {
          resolve(JSON.parse(res));
        });
      });
    },
    convertBase64UrlToBlob(urlData) {
      let bytes = window.atob(urlData.split(',')[1]);        //去掉url的头，并转换为byte
      //处理异常,将ascii码小于0的转换为大于0
      let ab = new ArrayBuffer(bytes.length);
      let ia = new Uint8Array(ab);
      for (let i = 0; i < bytes.length; i++) {
        ia[i] = bytes.charCodeAt(i);
      }
      return new Blob([ab], { type: 'image/png' });
    }
  }
}
</script>

<style lang="less" scoped>
@import "../assets/css/main";

.main-feed-back {
  .tip {
    background-color: #39C581;
    color: #ffffff;
    font-size: 14px;
    height: 40px;
    line-height: 40px;
    padding-left: 16px;
    margin-bottom: 8px;
  }

  .gap {
    height: 8px;
  }

  .text {
    background-color: #ffffff;
    padding: 8px 16px;
    color: #2D3632;
    font-size: 16px;
    line-height: 24px;
  }

  .divider {
    margin: 0;
  }
}

.btn {
  padding: 16px;
  background: #EEEEEE;
}

.feedback_img {
  padding: 5px 14px;
  position: relative;
  background-color: #ffffff;

  .feedback_item {
    padding: 5px;
    text-align: center;
    width: 80px;
    height: 100px;
    float: left;
    overflow: hidden;

    .img {
      color: #BBBBBB;
      width: 100%;
      height: 100%;
      text-align: right;

      .feedback_item_img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      .feedback_item_close {
        font-size: 20px;
        position: absolute;
        margin-right: 0;
        z-index: 1000;
        background-color: #ffffff;
        border-radius: 20px;
      }
    }

    .add {
      float: left;
      width: 100%;
      height: 100%;
      color: #BBBBBB;
      border: 1px #BBBBBB solid;

      .feedback_icon {
        font-size: 50px;
        line-height: 70px;
      }
    }
  }
}

.img-length {
  text-align: right;
  padding: 0 16px;
  background-color: #ffffff;
  font-size: 14px;
  font-weight: 400;
  color: #BBBBBB;
  line-height: 20px;
}
</style>